package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.BidEntity;
import com.ly.heart.domain.CustomerEntity;
import com.ly.heart.domain.TenderEntity;
import com.ly.heart.service.IBidService;
import com.ly.heart.service.ICustomerService;
import com.ly.heart.service.ITenderService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.TenderResultMapper;
import com.ly.heart.domain.TenderResultEntity;
import com.ly.heart.service.ITenderResultService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 投标结果Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class TenderResultServiceImpl extends ServiceImpl<TenderResultMapper,TenderResultEntity> implements ITenderResultService
{
    @Resource
    private TenderResultMapper tenderResultMapper;
    @Resource
    private IBidService bidService;
    @Resource
    private ITenderService tenderService;
    @Resource
    private ICustomerService customerInfoService;

    @Override
    public TenderResultEntity selectTenderResultVoById(String id){
        TenderResultEntity tenderResultEntity = this.getById(id);
        Optional.ofNullable(tenderResultEntity).ifPresent(tenderResult -> {
            BidEntity bidEntity = bidService.getById(tenderResult.getBidId());
            Optional.ofNullable(bidEntity).ifPresent(bid -> {
                tenderResult.setBid(bid);
                CustomerEntity customer = customerInfoService.getById(bid.getCustomerId());
                tenderResult.setCustomer(Optional.ofNullable(customer).orElse(new CustomerEntity()));
            });
            TenderEntity tenderEntity = tenderService.getById(tenderResult.getTenderId());
            tenderResult.setTender(Optional.ofNullable(tenderEntity).orElse(new TenderEntity()));
        });
        return tenderResultEntity;
    }

    @Override
    public AjaxResult selectTenderResultVoList(TenderResultEntity tenderResultEntity){
        List<TenderResultEntity> tenderResultList = tenderResultMapper.selectTenderResultEntityList(tenderResultEntity);
        if(!CollectionUtils.isEmpty(tenderResultList)){
            tenderResultList.forEach(tenderResult -> {
                BidEntity bidEntity = bidService.getById(tenderResult.getBidId());
                Optional.ofNullable(bidEntity).ifPresent(bid -> {
                    tenderResult.setBid(bid);
                    CustomerEntity customer = customerInfoService.getById(bid.getCustomerId());
                    tenderResult.setCustomer(Optional.ofNullable(customer).orElse(new CustomerEntity()));
                });
                TenderEntity tenderEntity = tenderService.getById(tenderResult.getTenderId());
                tenderResult.setTender(Optional.ofNullable(tenderEntity).orElse(new TenderEntity()));
            });
        }
        Page<TenderResultEntity> page = new Page<>(tenderResultEntity.getPage(),tenderResultEntity.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,tenderResultList));
    }

    /**
     * 查询投标结果
     *
     * @param id 投标结果主键
     * @return 投标结果
     */
    @Override
    public TenderResultEntity selectTenderResultEntityById(String id)
    {
        return tenderResultMapper.selectTenderResultEntityById(id);
    }

    /**
     * 查询投标结果列表
     *
     * @param tenderResultEntity 投标结果
     * @return 投标结果
     */
    @Override
    public List<TenderResultEntity> selectTenderResultEntityList(TenderResultEntity tenderResultEntity)
    {
        return tenderResultMapper.selectTenderResultEntityList(tenderResultEntity);
    }

    /**
     * 新增投标结果
     *
     * @param tenderResultEntity 投标结果
     * @return 结果
     */
    @Override
    public int insertTenderResultEntity(TenderResultEntity tenderResultEntity)
    {
        tenderResultEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
        tenderResultEntity.setCreateTime(DateUtils.getNowDate());
        return tenderResultMapper.insertTenderResultEntity(tenderResultEntity);
    }

    /**
     * 修改投标结果
     *
     * @param tenderResultEntity 投标结果
     * @return 结果
     */
    @Override
    public int updateTenderResultEntity(TenderResultEntity tenderResultEntity)
    {
        tenderResultEntity.setChangeTime(DateUtils.getNowDate());
        return tenderResultMapper.updateTenderResultEntity(tenderResultEntity);
    }

    /**
     * 批量删除投标结果
     *
     * @param ids 需要删除的投标结果主键
     * @return 结果
     */
    @Override
    public int deleteTenderResultEntityByIds(String[] ids)
    {
        return tenderResultMapper.deleteTenderResultEntityByIds(ids);
    }

    /**
     * 删除投标结果信息
     *
     * @param id 投标结果主键
     * @return 结果
     */
    @Override
    public int deleteTenderResultEntityById(String id)
    {
        return tenderResultMapper.deleteTenderResultEntityById(id);
    }
}
